<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title> 拍卖列表 </title>
    <link href="css/common.css" rel="stylesheet" type="text/css"/>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="wrap">
    <!-- main begin-->
    <div class="sale">
        <h1 class="lf">在线拍卖系统</h1>
        <div class="logout right"><a href="#" title="注销">注销</a></div>
    </div>
    <div class="forms">
        <form action="${pageContext.request.contextPath}/do/pageList" method="get">
            <input type="hidden" name="pageNum" value="1"/>
            <input type="hidden" name="pageSize" value="5"/>

            <label for="name">名称</label>
            <input name="auctionName" type="text" class="nwinput" id="name"/>
            <label for="names">描述</label>
            <input name="auctionDesc" type="text" id="names" class="nwinput"/>
            <label for="time">开始时间</label>
            <input name="auctionStartTime" type="text" id="time" class="nwinput"/>
            <label for="end-time">结束时间</label>
            <input name="auctionEndTime" type="text" id="end-time" class="nwinput"/>
            <label for="price">起拍价</label>
            <input name="auctionStartPrice" type="text" id="price" class="nwinput"/>
            <input type="submit" value="查询" class="spbg buttombg f14 sale-buttom"
                   onclick="goPage(1); return false;"/>
        </form>
    </div>
    <!-- 数据显示 -->
    <div class="items">
        <c:if test="${not empty error}">
            <div class="error">${error}</div>
        </c:if>

        <c:if test="${not empty pageInfo}">
            <!-- 表格标题 -->
            <ul class="rows even strong">
                <li>名称</li>
                <li>描述</li>
                <li>开始时间</li>
                <li>结束时间</li>
                <li>起拍价</li>
                <li>操作</li>
            </ul>

            <!-- 数据行 -->
            <c:forEach items="${pageInfo.list}" var="auction" varStatus="status">
                <ul class="rows ${status.index % 2 == 0 ? 'even' : ''}">
                    <li>${auction.auctionName}</li>
                    <li>${auction.auctionDesc}</li>
                    <li><fmt:formatDate value="${auction.auctionStartTime}" pattern="yyyy-MM-dd"/></li>
                    <li><fmt:formatDate value="${auction.auctionEndTime}" pattern="yyyy-MM-dd"/></li>
                    <li><fmt:formatNumber value="${auction.auctionStartPrice}" type="currency"/></li>
                    <li class="borderno red">
                        <a href="bid.jsp?auctionId=${auction.auctionId}">竞拍</a>
                    </li>
                </ul>
            </c:forEach>

            <!-- 分页 -->
            <div class="page">
                <c:if test="${pageInfo.hasPreviousPage}">
                    <a href="javascript:goPage(${pageInfo.prePage})">上一页</a>
                </c:if>

                <c:forEach begin="1" end="${pageInfo.pages}" var="i">
                    <c:choose>
                        <c:when test="${i == pageInfo.pageNum}">
                            <span class="current">${i}</span>
                        </c:when>
                        <c:otherwise>
                            <a href="javascript:goPage(${i})">${i}</a>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>

                <c:if test="${pageInfo.hasNextPage}">
                    <a href="javascript:goPage(${pageInfo.nextPage})">下一页</a>
                </c:if>
            </div>
        </c:if>
    </div>
    <!-- main end-->
</div>
<script>
    function goPage(pageNum) {
        const form = document.createElement('form');
        form.method = 'get';
        form.action = '${pageContext.request.contextPath}/do/pageList';

        // 添加分页参数
        addHiddenInput(form, 'pageNum', pageNum);
        addHiddenInput(form, 'pageSize', ${pageInfo.pageSize});

        // 添加原有查询参数
        <c:forEach items="${queryParams}" var="param">
        <c:if test="${param.key != 'pageNum' && param.key != 'pageSize'}">
        addHiddenInput(form, '${param.key}', '${param.value[0]}');
        </c:if>
        </c:forEach>

        document.body.appendChild(form);
        form.submit();
    }

    function addHiddenInput(form, name, value) {
        const input = document.createElement('input');
        input.type = 'hidden';
        input.name = name;
        input.value = value;
        form.appendChild(input);
    }

</script>
</body>

</html>
